window.addEventListener('load',function() {
	var menu_courses		= document.getElementById('menu_courses');
	var menu_leaderboards	= document.getElementById('menu_leaderboards');
	var menu_achievement	= document.getElementById('menu_achievement');

	$('#user_rank_level').append("<h1>Level " + localStorage.getItem("estudia_level") + "</h1>");
	$('#user_rank_progressbar_detail').append("<h6>" + localStorage.getItem("estudia_exp") + " / " + "100" + "</h6>");
	
	$( "#user_rank_progressbar" ).progressbar({
      value: parseInt(localStorage.getItem("estudia_exp"))
    });
	
	localStorage.removeItem("estudia_additionalpoint");
	localStorage.removeItem("estudia_type");
	localStorage.removeItem("estudia_course");
	localStorage.removeItem("estudia_chapter");
	localStorage.removeItem("estudia_maxpage");
	localStorage.removeItem("estudia_ans_right");
	localStorage.removeItem("estudia_ans_false");
	localStorage.removeItem("estudia_currentpage");
	localStorage.removeItem("estudia_lesson_flag");
	/*
	for(var i = 1; i <= 11; i++) {
		if (i < 10) {
			alert(localStorage.getItem("estudia_achievment_b0" + i));
		} else {
			alert(localStorage.getItem("estudia_achievment_b" + i));
		}
	} // */
	
	/*
	alert("mat_lesson1" + " => " + localStorage.getItem("estudia_mat_lesson1"));
	alert("mat_lesson2" + " => " + localStorage.getItem("estudia_mat_lesson2"));
	alert("mat_quiz1" + " => " + localStorage.getItem("estudia_mat_quiz1"));	
	alert("mat_quiz2" + " => " + localStorage.getItem("estudia_mat_quiz2"));	
	alert("fis_lesson1" + " => " + localStorage.getItem("estudia_fis_lesson1"));
	alert("fis_lesson2" + " => " + localStorage.getItem("estudia_fis_lesson2"));
	alert("fis_lesson3" + " => " + localStorage.getItem("estudia_fis_lesson3"));
	alert("fis_quiz1" + " => " + localStorage.getItem("estudia_fis_quiz1"));	
	alert("fis_quiz2" + " => " + localStorage.getItem("estudia_fis_quiz2"));	
	alert("fis_quiz3" + " => " + localStorage.getItem("estudia_fis_quiz3"));	
	alert("bio_lesson1" + " => " + localStorage.getItem("estudia_bio_lesson1"));
	alert("bio_lesson2" + " => " + localStorage.getItem("estudia_bio_lesson2"));
	alert("bio_lesson3" + " => " + localStorage.getItem("estudia_bio_lesson3"));
	alert("bio_quiz1" + " => " + localStorage.getItem("estudia_bio_quiz1"));	
	alert("bio_quiz2" + " => " + localStorage.getItem("estudia_bio_quiz2"));	
	alert("bio_quiz3" + " => " + localStorage.getItem("estudia_bio_quiz3"));	// */
	
	
	menu_courses.onclick=function(){
		
		//random_lesson();
		//random_quiz();
		
		if ($("#challenge_quiz").text() == "Good!") {
			random_quiz();
		}
		
		if ($("#challenge_lesson").text() == "Good!") {
			random_lesson();
		}
		
		localStorage.removeItem("estudia_additionalpoint");
		localStorage.removeItem("estudia_type");
		localStorage.removeItem("estudia_course");
		localStorage.removeItem("estudia_chapter");
		localStorage.removeItem("estudia_maxpage");
		localStorage.removeItem("estudia_ans_right");
		localStorage.removeItem("estudia_ans_false");
		localStorage.removeItem("estudia_currentpage");
		localStorage.removeItem("estudia_lesson_flag");
		
		$(".middle_side").load("1-courses.html", function(data) {

		}); // */
	};
	
	menu_leaderboards.onclick=function(){

		$(".middle_side").load("1-leaderboards.html");
		//$.getScript("1-leaderboards.js"); // */
	};
	
	menu_achievement.onclick=function(){
		
		if (localStorage.getItem("estudia_achievment_b11") == "0") {
			checkSaviour(function(data) {
				if (data == true) {
					createPopUp("b11", "notificator");
				}
			});
		}
		
		$(".middle_side").load("1-achievement.html");
		//$.getScript("1-achievement.js"); // */
	};	
	
	$('#menu_userguide').click(function() {
		$(".middle_side").load("1-userguide.html");
	});
	
	random_lesson();
	random_quiz();
});

function random_quiz() {
	get_random("quiz", function(random) {
		var random_point = Math.floor((Math.random() * 11) + 10);
		
		var course 	= random.substr(0, random.length - 2);
		var chapter	= random.charAt(random.length - 1);
		
		$("#challenge_quiz").empty();
		$("#challenge_quiz").append("Kerjakan " + course + " bab " + chapter + "<br /><h2>(+" + random_point + " point)</h2>");
		
		$("#challenge_quiz").click(function() {
			//alert("asdasd");
			
			$.get( "../subbab.json", function( data ) {
			  
				var judul_bab = data[course][chapter];
				
				//alert(judul_bab);
				localStorage.setItem("estudia_additionalpoint", random_point);
				localStorage.setItem("estudia_type", "soal");
				localStorage.setItem("estudia_course", course);
				localStorage.setItem("estudia_chapter", chapter + "-" + judul_bab);
				localStorage.setItem("estudia_currentpage", 1);
				localStorage.setItem("estudia_ans_right", 0);
				localStorage.setItem("estudia_ans_false", 0);
			}).done(function() {
				$(".middle_side").load("tampilansoal.html", function(data) {

				}); // */
				
				$("#challenge_quiz").attr("disabled", "disabled").off('click');
				$("#challenge_quiz").empty();
				$("#challenge_quiz").append("<h1>Good!</h1>");
				
			});
		});
		
	});
}

function random_lesson() {
	get_random("lesson", function(random) {
		var random_point = Math.floor((Math.random() * 11) + 10);
		
		var course 	= random.substr(0, random.length - 2);
		var chapter	= random.charAt(random.length - 1);
		
		$("#challenge_lesson").empty();
		$("#challenge_lesson").append("Pelajari " + course + " bab " + chapter + "<br /><h2>(+" + random_point + " point)</h2>");
		
		$("#challenge_lesson").click(function() {
			//alert("asdasd");
			
			$.get( "../subbab.json", function( data ) {
			  
				var judul_bab = data[course][chapter];
				
				//alert(judul_bab);
				
				localStorage.setItem("estudia_lesson_flag", "true");
				localStorage.setItem("estudia_additionalpoint", random_point);
				localStorage.setItem("estudia_type", "soal");
				localStorage.setItem("estudia_course", course);
				localStorage.setItem("estudia_chapter", chapter + "-" + judul_bab);
				localStorage.setItem("estudia_currentpage", 1);
				localStorage.setItem("estudia_ans_right", 0);
				localStorage.setItem("estudia_ans_false", 0);
			}).done(function() {
				$.get( "../partmateri.json", function( data ) {
					maxPage = data[course][chapter];
					//alert(maxPage);
					localStorage.setItem("estudia_maxpage", maxPage);
				}).done(function() {
					$(".middle_side").load("tampilanmateri.html", function(data) {

					}); // */
					
					$("#challenge_lesson").attr("disabled", "disabled").off('click');
					$("#challenge_lesson").empty();
					$("#challenge_lesson").append("<h1>Good!</h1>");
				});
			});
		});
	});
}

function fill_array(type, callbackFunction) {
	
	var array = [];
	
	get_notyet("fisika", type, function(result) {
		//alert("fisika" + "=>" + result);
		if (result != 0) {
			array[array.length] = "fisika-" + result;
		}
		
		get_notyet("biologi", type, function(result) {
			//alert("biologi" + "=>" + result);
			if (result != 0) {
				array[array.length] = "biologi-" + result;
			}
			
			get_notyet("matematika", type, function(result) {
				//alert("matematika" + "=>" + result);
				if (result != 0) {
					array[array.length] = "matematika-" + result;
				}
				
				if ( callbackFunction ) {
					callbackFunction (array);
				}
			});
		});
	});
}

function get_random(type, callbackFunction) {
	
	fill_array(type, function(array){
		var final_random;
		if (array.length == 0) {
			var mapel = ["matematika", "biologi", "fisika"];
			var random_number = Math.floor((Math.random() * mapel.length));
			
			var picked_course = mapel[random_number];
			
			$.get( "../subbab.json", function( data ) {
				var max = data[picked_course]["length"];
				var random_chapter = Math.floor((Math.random() * max) + 1);
				
				final_random = picked_course + "-" + random_chapter;
				//alert(final_random);
				if ( callbackFunction ) {
					callbackFunction (final_random);
				}
			}).done(function() {

			});
			
		} else {
			var random_number = Math.floor((Math.random() * array.length));
			final_random = array[random_number];
			if ( callbackFunction ) {
				callbackFunction (final_random);
			}
		}
		
		
	});

}

function get_notyet(course, type, callbackFunction ) {
	
	//alert(course + "+" +type);
	
	var mapel;
	if ((course == "biologi") || (course == "matematika") || (course == "fisika")){
		mapel = course.substring(0, 3);
	} else  {
		mapel = "undefined";
	}
	
	var pattern;
	if ((mapel != "undefined")) {
		pattern = mapel + "_" + type;
		//alert(pattern);
	}
	
	var checker;
	var notyet = 0;
	$.get( "../subbab.json", function( data ) {
		var max = data[course]["length"];
		for (var i = 1; i <= max; i++) {
			checker = pattern + i;
			
			if(localStorage.getItem("estudia_" + checker) == "0") {
				notyet = i;
				break;
			}
		}
	
	//alert(notyet);
	//return notyet;
		if ( callbackFunction ) {
			callbackFunction (notyet);
		}
	
	}).done(function() {
	
	});
}
